package com.controller;

import com.config.ContextHolder;
import com.entity.Result;
import com.entity.User;
import com.entity.*;
import lombok.extern.slf4j.Slf4j;
import com.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@SuppressWarnings({"all"})
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    private  SearchParms searchParms;

    @PostMapping("/login")
    public Result login(User user) {
        try {
            return Result.success(userService.login(user));
        } catch (Exception e) {
            return Result.error(userService.login(user));
        }
    }

    @PostMapping("/hasRegister")
    public Result hasRegister(User user) {
        try {
            return Result.success(userService.hasRegister(user.getMobile()));
        } catch (Exception e) {
            return Result.error("hasRegister的未知错误");
        }
    }

    @PostMapping("/register")
    public Result register(User user) {
        try {
            return Result.success(userService.register(user));
        } catch (Exception e) {
            return Result.error(userService.register(user));
        }

    }
    @PostMapping("/search")
    public Result getByCT( SearchParms searchParms){
        log.info("按校区和类型查询场地");
        if(searchParms.cCampus.equals("全部") && searchParms.cType.equals("全部")){
            System.out.println("进入一");
            pageBean ground= userService.getAllGround(searchParms.pageNum,searchParms.pageSize);
            return  Result.success(ground);

        }else if (!searchParms.cCampus.equals("全部") && !searchParms.cType.equals("全部")){
            System.out.println("进入二");
            pageBean ground = userService.search(searchParms.cCampus,searchParms.cType,searchParms.pageNum,searchParms.pageSize);
            return  Result.success(ground);

        }else if (searchParms.cCampus.equals("全部") && !searchParms.cType.equals("全部")){
            System.out.println("进入三");
            pageBean ground = userService.searchByType(searchParms.cType,searchParms.pageNum,searchParms.pageSize);
            return  Result.success(ground);

        }else {
            System.out.println("进入四");
            pageBean ground = userService.searchByCampus(searchParms.cCampus,searchParms.pageNum,searchParms.pageSize);
            return  Result.success(ground);
        }
    }

    @PostMapping("/save")
    public Result saveOrUpdate(RuleForm ruleForm){
        System.out.println("进入saveOrUpdate 参数为:"+ruleForm.getG_id()+" "+ruleForm.getT_id()+" "+ruleForm.getP_date());
        User user = userService.info(ContextHolder.getMobile().toString());
        try{
            return Result.success(userService.addPreorder(ruleForm, user.getId()));
        }catch (Exception e){
            return Result.error("该时间已被预约，请选择其他时间段");
        }
    }


    @PostMapping("/info")
    public Result info() {
        try {
            System.out.println("进入控制层的info，当前数据为：" + ContextHolder.getMobile().toString());
            return Result.success(userService.info(ContextHolder.getMobile().toString()));
        } catch (Exception e) {
            return Result.error("info报错报错未知错误"+e.getMessage());
        }
    }

    @PostMapping("/forget")
    public Result forget(User user){
        try {
            return Result.success(userService.forget(user));
        } catch (Exception e) {
            return Result.error("找回密码操作发生未知错误"+e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(User user) {
        try {
            return Result.success(userService.update(user));
        } catch (Exception e) {
            return Result.error("updata报错报错未知错误"+e.getMessage());
        }
    }

    @PostMapping("/PInfo")
    public Result PInfo(User user) {
        try {
            return Result.success(userService.PInfo(user.getUname()));
        } catch (Exception e) {
            return Result.error("PInfo报错报错未知错误"+e.getMessage());
        }
    }

    @PostMapping("/updateP")
    public Result updateP(updateP updatep){
        System.out.println("进入updateP, 参数为:"+updatep.getGID()+" "+updatep.getTID()+" "+updatep.getPDATE());
        try {
            return Result.success(userService.updateP(updatep.getGID(), updatep.getTID(), updatep.getPDATE()));
        } catch (Exception e) {
            return Result.error("PInfo报错报错未知错误"+e.getMessage());
        }
    }

    @PostMapping("/autoLoad")
    public Result autoLoad(){
        try{
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
